<?php
/* 
 * ebiz front
 * 
 * Promotion Dao
 * 
 * @author fusq
 * 
 */

require_once '../app/core/models/BaseDao.php';
require_once '../app/core/db/DBUtil.class.php';

class OrderPromotionDao extends BaseDao {
	
    private $tableName = "order_promotions";
    
    public function __construct($dbUtil){   
        parent::__construct($dbUtil);            
    }
    
    /**
     * insert order promotion
     * @param $orderPromotion
     */
    public function saveOrderPromotion($param){
    	$sql = "insert into $this->tableName (`order_id`,`promotion_id`,`sequence`,`created`,`modified`) values (?,?,?,now(),now()) ";
    	return $this->dbUtil->ExecuteQuery($sql, $param);
    }
    
    public function getUsedCountByCustomerAndPromotion($userId,$promotionId){
    	$param = array($userId,$promotionId);
    	$sql = "select count(op.id) as used_count from $this->tableName op 
    	       left outer join orders o on o.id = op.order_id and o.is_delete = 0 
    	       where o.user_id = ? and op.promotion_id = ? and op.is_delete = 0 ";
    	$result = $this->dbUtil->SelectData($sql, $param);
    	return $result[0]['used_count'];
    }
}